﻿using AutoMapper;
using B.S.Database.Domain.Entity;
using B.S.Database.ErrorCode;
using B.S.Database.Interface.Interface;
using B.S.Database.Write.Api.Application.Commands.Breeding;
using MediatR;

namespace B.S.Database.Write.Api.Application.CommandHandler.Breeding
{
    public class ImmuneCreateCommandHandler : IRequestHandler<ImmuneCreateCommands, Apiresult<object>>
    {
        private readonly IBaseResotity<Immune_Infomation> immuneRepo;
        private readonly IMapper mapper;

        public ImmuneCreateCommandHandler(IBaseResotity<Immune_Infomation> ImmuneRepo, IMapper mapper)
        {
            immuneRepo = ImmuneRepo;
            this.mapper = mapper;
        }
        public Task<Apiresult<object>> Handle(ImmuneCreateCommands request, CancellationToken cancellationToken)
        {
            Apiresult<object> res = new Apiresult<object>();
            using (var tran = immuneRepo.Db.Database.BeginTransaction())
            {
                try
                {
                        // 添加
                        var list = mapper.Map<Immune_Infomation>(request);
                        immuneRepo.Add(list);
                        res.Code = ApiEnums.成功;
                        res.Message = "添加成功";

                    tran.Commit();

                }
                catch (Exception)
                {
                    res.Code = ApiEnums.错误;
                    res.Message = ApiEnums.错误.ToString();
                    tran.Rollback();
                    throw;
                }
            }
            return Task.FromResult(res);
        }
    }
}
